package com.bd.learning.excel;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import java.util.*;

public class ExcelGroup {


    public static void main(String[] args) {
        ExcelGroup eg = new ExcelGroup();
        List<BeanObj> totalList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\SMCP对外类汇总.xlsx", 2);
        List<BeanObj> aqList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\安全管理-付兴乐.xlsx", 2);
        List<BeanObj> reportList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\报表管理-王萌.xlsx", 2);
        List<BeanObj> alarmList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\告警管理-熊梦飞.xlsx", 2);
        List<BeanObj> baseConfList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\基础配置-付兴乐.xlsx", 2);
        List<BeanObj> baseChanelList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\基础通道-付兴乐.xlsx", 2);
        List<BeanObj> monitorList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\监管中心-蒙秋敏.xlsx", 2);
        List<BeanObj> dbList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\数据库转储-张兴伦.xlsx", 2);
        List<BeanObj> txList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\通信管理-张合.xlsx", 2);
        List<BeanObj> pmCurrentList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\性能管理-史红艳.xlsx", 2);
        List<BeanObj> pmHisList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\性能管理-向乐.xlsx", 2);
        List<BeanObj> bussList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\业务管理-刘丹.xlsx", 2);
        List<BeanObj> busList = eg.getBeanObj("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\总线微服务-刘洋.xlsx", 2);

        List<BeanObj> doList = new ArrayList<>();
        eg.unionAll(totalList, doList, aqList, "安全管理");
        eg.unionAll(totalList, doList, reportList, "报表管理");
        eg.unionAll(totalList, doList, alarmList, "告警管理");
        eg.unionAll(totalList, doList, baseConfList, "基础配置");
        eg.unionAll(totalList, doList, baseChanelList, "基础通道");
        eg.unionAll(totalList, doList, monitorList, "监管中心");
        eg.unionAll(totalList, doList, dbList, "数据库转储");
        eg.unionAll(totalList, doList, txList, "通信管理");
        eg.unionAll(totalList, doList, pmCurrentList, "性能管理");
        eg.unionAll(totalList, doList, pmHisList, "性能管理");
        eg.unionAll(totalList, doList, bussList, "业务管理");
        eg.unionAll(totalList, doList, busList, "总线微服务");

        Map<String, String> map = eg.getMap("D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\modulemapping.xlsx", 0);
        String doFile = "D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\1-已分配接口.xlsx";
        String undoFile = "D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\1-未分配接口.xlsx";
        eg.checkFiled(doList);
        eg.writeExcel(doFile, doList);
        totalList.remove(0);
        for (BeanObj obj : totalList) {
            if (StrUtil.isEmpty(obj.getServerName())) {
                obj.setServerName(map.get(obj.getPackageName()));
            }
        }
        eg.checkFiled(totalList);
        eg.writeExcel(undoFile, totalList);

        List<BeanObj> allList = new ArrayList<>();
        allList.addAll(doList);
        allList.addAll(totalList);
        String allFile = "D:\\doc\\测试覆盖率\\测试代码覆盖率提升\\1-全分类接口.xlsx";
        System.out.println(totalList.size() + "|" + doList.size());
        eg.writeExcel(allFile, allList);
    }


    public void checkFiled(List<BeanObj> list){
        for (BeanObj obj:list){
            if(StrUtil.trimToNull(obj.getPackageName()) == null || "null".equals(StrUtil.trimToEmpty(obj.getPackageName()))){
                obj.setPackageName("");
            }
            if(StrUtil.trimToNull(obj.getMethodNum()) == null || "null".equals(StrUtil.trimToEmpty(obj.getMethodNum()))){
                obj.setMethodNum("0");
            }
        }
    }

    public Map<String, String> getMap(String file, int index) {
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(file), index);
        List<List<Object>> readAll = reader.read();
        Map<String, String> map = new HashMap<>();
        for (List<Object> list : readAll) {
            map.put(String.valueOf(list.get(0)), String.valueOf(list.get(1)));
        }
        reader.close();
        return map;
    }

    private void writeExcel(String file, List<BeanObj> list) {
        ExcelWriter writer = ExcelUtil.getWriter(file);
        writer.write(list, false);
        writer.flush();
        writer.close();
    }

    public List<BeanObj> getBeanObj(String file, int index) {
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file(file), index);
        List<List<Object>> readAll = reader.read();
        List<BeanObj> totalList = new ArrayList<>();
        for (List<Object> list : readAll) {
            BeanObj bo = new BeanObj();
            bo.setType1((String) list.get(0));
            bo.setType2((String) list.get(1));
            bo.setPackageName(String.valueOf(list.get(2)));
            bo.setFileName(String.valueOf(list.get(3)));
            bo.setMethodName(String.valueOf(list.get(4)));
            bo.setMethodNum(String.valueOf(list.get(5)));
            totalList.add(bo);
        }
        reader.close();
        return totalList;
    }

    private void unionAll(List<BeanObj> totalList, List<BeanObj> doList, List<BeanObj> tmp, String serverName) {
        for (BeanObj obj : tmp) {
            boolean remove = totalList.remove(obj);
            if (remove) {
                obj.setServerName(serverName);
                doList.add(obj);
            }
        }
    }


}
